package dk.marten.ibdrecord;

import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.SeekBar;
import android.widget.TimePicker;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import android.app.Dialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;


public class RedigerTilstand extends Activity implements OnSeekBarChangeListener {
	
	private SeekBar sliderTilstand, sliderFatigue, sliderSmerte, sliderStress;
	
	private TextView  weightTekst, tilstandValue, fatigueValue, smerteValue, stressValue, datoTekst, tidsTekst;
		
	private DBAccess dBAccess;

	private TilstandData tilstand;
		
	private String[] tilstandSteps;
	private String[] generelSteps;
	
	long id = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rediger_tilstand);
		
		
		if (getIntent().hasExtra("id")){
			id = getIntent().getExtras().getLong("id");
		}
		
		dBAccess = new DBAccess(this);

		tilstandValue = (TextView) findViewById(R.id.tilstandValue);
		fatigueValue = (TextView) findViewById(R.id.fatigueValue);
		smerteValue = (TextView) findViewById(R.id.smerteValue);
		stressValue = (TextView) findViewById(R.id.stressValue);
		datoTekst = (TextView) findViewById(R.id.datoTekst);
		tidsTekst = (TextView) findViewById(R.id.tidsTekst);
		weightTekst= (TextView) findViewById(R.id.weightTekst);
		
		sliderTilstand = (SeekBar) findViewById(R.id.sliderTilstand);
		sliderFatigue = (SeekBar) findViewById(R.id.sliderFatigue);
		sliderSmerte = (SeekBar) findViewById(R.id.sliderSmerte);
		sliderStress = (SeekBar) findViewById(R.id.sliderStress);

		sliderTilstand.setOnSeekBarChangeListener(this);
		sliderFatigue.setOnSeekBarChangeListener(this);
		sliderSmerte.setOnSeekBarChangeListener(this);
		sliderStress.setOnSeekBarChangeListener(this);
		
		Resources res = getResources();
		tilstandSteps = res.getStringArray(R.array.tilstandSteps);
		generelSteps = res.getStringArray(R.array.generelSteps);

		//sliderTilstand.setVisibility(View.GONE);
		
		tilstand = new TilstandData();
		
		if(id != -1){
			tilstand = dBAccess.getTilstand(id);
		}

		sliderTilstand.setProgress(tilstand.getTilstand());
		sliderFatigue.setProgress(tilstand.getFatigue());
		sliderSmerte.setProgress(tilstand.getSmerte());
		sliderStress.setProgress(tilstand.getStress());
		weightTekst.setText(""+tilstand.getWeight());
		
		datoTekst.setText(DateFormat.format("dd/MM/yyyy", tilstand.getDato()));
		tidsTekst.setText(DateFormat.format("h:mm", tilstand.getDato()));

		updateSeekbars();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_rediger_tilstand, menu);
		
		if(id != -1){
			MenuItem btn = (MenuItem) menu.getItem(2);
			if(btn != null) btn.setVisible(false);
		}
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.redTilstandGemBtn:
			gemData();
			break;
		case R.id.redTilstandCancelBtn:
			finish();
			break;
		case R.id.redTilstandDeleteBtn:
			sletData();
			break;
		
		}
        return true;
    }
	
	
    private void sletData() {
		// TODO Slet data
		finish();
		
	}

	@Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    	switch(seekBar.getId()){
	    	case R.id.sliderTilstand:
	    	case R.id.sliderFatigue:
	    	case R.id.sliderSmerte:
	    	case R.id.sliderStress:
	    		updateSeekbars();
	    	break;
    	}
    }
    
    private void updateSeekbars(){
    	tilstandValue.setText(tilstandSteps[sliderTilstand.getProgress()]);
    	fatigueValue.setText(generelSteps[sliderFatigue.getProgress()]);
    	smerteValue.setText(generelSteps[sliderSmerte.getProgress()]);
    	stressValue.setText(generelSteps[sliderStress.getProgress()]);
    	
    }
    
	public void redigerDato(View view) {
		showDialog(1);
	 }
	    
	 public void redigerTid(View view) {
		showDialog(2);
	 }
	    
	 public void gemData(View view) {
		 gemData();
	 
	 }
	 public void gemData() {
		 double weight = Double.parseDouble(weightTekst.getText().toString().trim());
		 tilstand.setWeight(weight);
		 tilstand.setFatigue(sliderFatigue.getProgress());
		 tilstand.setSmerte(sliderSmerte.getProgress());
		 tilstand.setStress(sliderStress.getProgress());
		 tilstand.setTilstand(sliderTilstand.getProgress());
		 
	    Log.d("MOE","tilstand: "+tilstand);

		dBAccess.insertOrUpdateTilstandAsync(tilstand);
		this.finish();
	 }

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Calendar regDato = tilstand.getDato();
		switch (id) {
		case 1:
			return new DatePickerDialog(this, datePickerListener, regDato.get(Calendar.YEAR), regDato.get(Calendar.MONTH),regDato.get(Calendar.DAY_OF_MONTH));
		case 2:
			return new TimePickerDialog(this, timePickerListener, regDato.get(Calendar.HOUR_OF_DAY), regDato.get(Calendar.MINUTE),false);
		}
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 
		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int year, int month, int day) {
			Calendar regDato = tilstand.getDato();
			regDato.set(Calendar.YEAR, year);
			regDato.set(Calendar.MONTH, month);
			regDato.set(Calendar.DAY_OF_MONTH, day);
			datoTekst.setText(DateFormat.format("dd/MM/yyyy", regDato));
			tilstand.setDato(regDato);
		}
	};
	
	private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {	
			Calendar regDato = tilstand.getDato();
			regDato.set(Calendar.HOUR_OF_DAY, hourOfDay);
			regDato.set(Calendar.MINUTE, minute);
			tidsTekst.setText(DateFormat.format("h:mm", regDato));
			tilstand.setDato(regDato);
		}
	};
	

}
